package com.example.mystore.mapper;

import com.example.mystore.model.User;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface UserMapper {

    @Insert("INSERT INTO user (account, password, email, phone, question, asw, role, age, sex, create_time, update_time, del, name) " +
            "VALUES (#{account}, #{password}, #{email}, #{phone}, #{question}, #{asw}, #{role}, #{age}, #{sex}, #{createTime}, #{updateTime}, #{del}, #{name})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insertUser(User user);

    // 逻辑删除的方法
    @Update("UPDATE user SET del = 1 WHERE id = #{id}")
    int markUserAsDeleted(int id);

    @Update("UPDATE user SET account = #{account}, password = #{password}, email = #{email}, phone = #{phone}, " +
            "question = #{question}, asw = #{asw}, role = #{role}, age = #{age}, sex = #{sex}, create_time = #{createTime}, " +
            "update_time = #{updateTime}, del = #{del}, name = #{name} WHERE id = #{id}")
    int updateUser(User user);

    @Select("SELECT * FROM user WHERE id = #{id}")
    User selectUserById(int id);

    @Select("SELECT * FROM user WHERE account = #{account} LIMIT 1")
    User getUserByUsername(String account);

    @Select("SELECT * FROM user WHERE del = 0")
    List<User> selectAllUsers();

    @Select("SELECT * FROM user WHERE del = 0 AND id = #{uid}")
    User getUserById(Integer uid);

    @Update("UPDATE user SET password = #{password} WHERE account = #{account}")
    int updatePasswordByAccount(String account, String password);
    @Update("UPDATE user SET password = #{password}, update_time = #{updateTime} WHERE id = #{id}")
    int updatePassword(@Param("id") Integer id, @Param("password") String password, @Param("updateTime") String updateTime);



}

